#include "EventManager.h"
#include <windows.h>

bool EventManager::AddListener(Listener listener)
{
	//Check for duplicates before adding the listener
	if (SearchListenerList(listener)==false)
	{
		ListenerList.push_front(listener);
		return true;
	}
	return false;
}


bool EventManager::DeleteListener(Listener listener)
{
	//Traverse the ListenerList looking for the passed in listener
	std::list<Listener>::iterator Itor = ListenerList.begin();

	while (Itor != ListenerList.end())
	{
		//If you find it, delete it and return true
		if (!strcmp(Itor->GetName(),listener.GetName()))
		{
			ListenerList.erase(Itor);
			return true;
		}
	}
	//If you can't find it, return false
	return false;	
}

bool EventManager::QueueEvent(EventType event)
{
	if(!ValidateEvent(event))
	{
		return false;
	}
	if (EventQueue.size() == 0)
	{
		EventQueue.push_back(event);
		return true;
	}
	if (strcmp(event.getName(),EventQueue.back().getName())==0)
	{
		EventQueue.pop_back();
		EventQueue.push_back(event);
		return true;
	}
	else
	{
		EventQueue.push_back(event);
		return true;
	}
	return false;
}

bool EventManager::QueueEvent(char eventName[])
{
	//Create the event to add based on the passed in char
	EventType event(eventName);

	if (EventQueue.size() == 0)
	{
		EventQueue.push_back(event);
		return true;
	}
	if (strcmp(eventName,EventQueue.back().getName())==0)
	{
		EventQueue.pop_back();
		EventQueue.push_back(event);
	}
	else
	{
		EventQueue.push_back(event);
		return true;
	}
	return false;
}
bool EventManager::DeleteEvent(EventType event)
{
	//Validate the event to check for?
	//Traverse the EventQueue list looking for the event
	std::deque<EventType>::iterator Itor = EventQueue.begin();
	while (Itor != EventQueue.end())
	{
		//If you find the event, erase it and return true
		if (!strcmp(Itor->getName(),event.getName()))
		{
			EventQueue.erase(Itor);
		}	
		//Upon not finding, return false
		return false;
	}
}
bool EventManager::DeleteEvent(char eventName[])
{
	//Validate the event to check for?
	//Traverse the EventQueue list looking for the event
	std::deque<EventType>::iterator Itor = EventQueue.begin();
	while (Itor != EventQueue.end())
	{
		//If you find the event, erase it and return true
		if (!strcmp(Itor->getName(),eventName))
		{
			EventQueue.erase(Itor);
		}	
	}
	//Upon not finding, return false
	return false;
}
bool EventManager::ValidateEvent(EventType event)
{
	std::list<EventType>::iterator Itor = ValidEvents.begin();

	while(Itor != ValidEvents.end())
	{
		if (!strcmp(event.getName(),Itor->getName()))
		{
			return true;
		}
		Itor++;
	}
	return false;
}
bool EventManager::ValidateEvent(char eventName[])
{
	std::list<EventType>::iterator Itor = ValidEvents.begin();

	while(Itor != ValidEvents.end())
	{
		if (!strcmp(eventName,Itor->getName()))
		{
			return true;
		}
		Itor++;
	}
	return false;
}
bool EventManager::SearchListenerList(Listener listener)
{
	//Check for an empty list
	if (ListenerList.size() == 0)
	{
		return false;
	}

	std::list<Listener>::iterator Itor = ListenerList.begin();

	while(Itor != ListenerList.end())
	{
		if (!strcmp(listener.GetName(),Itor->GetName()))
		{
			return true;
		}
		Itor++;
	}
	return false;
}
void EventManager::PopulateValidEvents()
{
	std::ifstream inFile("ValidEvents.txt");
	char buffer;
	char *EventName = new char;
	int x=0;

	while (!inFile.eof())
	{
		buffer = inFile.get();
		while (buffer != '\n' && buffer !='\r' && !inFile.eof())
		{
			EventName[x] = buffer;
			buffer = inFile.get();
			x++;
		}
		EventType event;
		event.setName(EventName);
		x=0;
		ValidEvents.push_back(event);
	}
}
void EventManager::ProcessEvents()
{
	//Only run for 5ms, getting called every gameloop
	int endTime = GetTickCount()+5;

	//Push all of EventQueue to ActiveQueue
	//Iterate through ActiveQueue until finished or endTime reached
	while(EventQueue.size()>0)
	{
		ActiveEvents.push_back(EventQueue.back());
		EventQueue.pop_back();
	}
	
	std::list<Listener>::iterator Itor = ListenerList.begin();

	while ((ActiveEvents.size() > 0)&&(Itor != ListenerList.end()))
	{
		if (GetTickCount() > endTime)
		{
			return;
		}

		if (Itor->HandleEvent(ActiveEvents.back())==true)
		{
			ActiveEvents.pop_back();
		}
		else
		{
			Itor++;
		}
	}
}